<?php
//2011-12-08
require_once( "../classes/mofachi.Baby.php" );
$baby = new Baby();
@ $uid = $_POST[ "uid" ];
@ $baby->nickname = $_POST[ "nn" ];
@ $baby->realname = $_POST[ "rn" ];
@ $baby->gender = $_POST[ "gd" ];
@ $baby->birthday = $_POST[ "br" ];

@ $region_code = $_POST[ "rc" ];
@ $city_code = $_POST[ "cc" ];

header( 'Content-Type: text/plain; charset=utf-8' );
if( !$uid || !$baby->nickname ){
	echo utf8_encode( "{\"reply\":-1,\"msg\":\"ERROR: No user id or baby's nickname\"}" );
	exit();
}
$baby->loadByNickname();
if( $baby->isLoaded() ) {
	echo utf8_encode( "{\"reply\":0,\"msg\":\"ERROR: The nickname has been used\"}" );
	exit();
}
$baby->location_id = "";
if( $city_code ) {
	$loc = new Location();
	$loc->city_code = $city_code;
	if( $loc->loadByCityCode() )
		$baby->location_id = $loc->location_id;
} else if( $region_code ) {
	$loc = new Location();
	$loc->region_code = $region_code;
	if( $loc->loadByRegionCode() )
		$baby->location_id = $loc->location_id;
}
$baby->add();

$baby->loadByNickName();

require_once( "../classes/mofachi.UserBaby.php" );
$user_baby = new UserBaby();
$user_baby->user_id = $uid;
$user_baby->baby_id = $baby->baby_id;
$user_baby->relation= 7;
$res = $user_baby->add();
if( $res ) {
	echo utf8_encode( "{\"reply\":".$baby->baby_id.",\"msg\":\"Success: The baby information has been added!\"}" );
}
?>